﻿using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Discovery;
using Nardax.ServiceModel.Discovery.Bindings;

namespace Nardax.ServiceModel.Discovery
{
    public class BindingDiscoveryEndpointBehavior : IEndpointBehavior
    {
        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
        {
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
        {
            var endpointDiscoveryBehavior = endpoint.Behaviors.Find<EndpointDiscoveryBehavior>();

            if (endpointDiscoveryBehavior == null)
            {
                endpointDiscoveryBehavior = new EndpointDiscoveryBehavior();
                endpoint.Behaviors.Add(endpointDiscoveryBehavior);
            }

            endpointDiscoveryBehavior.Extensions.Add(BindingSerializer.Serialize(endpoint.Binding));
        }

        public void Validate(ServiceEndpoint endpoint)
        {
        }
    }
}